// Windows Header Files:
#include <windows.h>

// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

#include "XgWin.h"
#include "Templates/XgSignal.h"
#include "XgThread.h"

/************************************************************************/
/*                                                                      */
/************************************************************************/
class Test
{
public:
	void slot(int)
	{
		OutputDebugString(_T("Test::slot(int) called\n"));
	}

	virtual void doSignal()
	{
		aSignal.emit(1);
	}

public:
	signal(int) aSignal;
};

/************************************************************************/
/*                                                                      */
/************************************************************************/
class TestEx : public Test
{
public:
	void slot(int)
	{
		OutputDebugString(_T("TestEx::slot(int) called\n"));
	}
};

void slotf(int)
{
	OutputDebugString(_T("slotf(int) called\n"));
}

/************************************************************************/
/*                                                                      */
/************************************************************************/
class MyThread : public XgThread
{
public:
	void run()
	{
		OutputDebugString(_T("Running new thread\n"));

		// enter the messages loop
		exec();
	}
};

/************************************************************************/
/*                                                                      */
/************************************************************************/
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
 	Test* obj = new Test;

	MyThread* mythread = new MyThread;
	mythread->create();

	// connect aSignal with the method obj->slot
	obj->aSignal.connect(obj, &Test::slot);

    // connect aSignal with the method obj->slot but in other thread
	obj->aSignal.connect(obj, &Test::slot, mythread);

	// connect aSignal with the function slotf
	obj->aSignal.connect(&slotf);

	// wait for thread to create the messages queue
	mythread->onMessagesQueueCreate.wait();

	// emit the signal
	obj->doSignal();

	MSG msg;
	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return (int) msg.wParam;

	return 0;
}